/*
 * Author: Karthic Kumar Sekar [A0092622]
 */
package sg.edu.nus.iss.team4ft.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Label;
import java.awt.List;
import java.awt.Panel;
import java.util.ArrayList;

import javax.swing.BoxLayout;

import sg.edu.nus.iss.team4ft.service.Account;
import sg.edu.nus.iss.team4ft.service.Feature;
import sg.edu.nus.iss.team4ft.service.Plan;
import sg.edu.nus.iss.team4ft.service.PlanCode;
import sg.edu.nus.iss.team4ft.util.AppConstant;

public class AccountHistoryView extends Panel {
	
	private static final long serialVersionUID = 1L;

	public AccountHistoryView(Account account) throws CloneNotSupportedException{
		if(account!= null)
			loadControls(account);
	}

	private void loadControls(Account account) throws CloneNotSupportedException {
		Dimension d = new java.awt.Dimension(550, 180);
		this.setPreferredSize(new java.awt.Dimension(700, 900));
	
		ArrayList<String> mobileActive = new ArrayList<String>();
		ArrayList<String> mobileDeactive = new ArrayList<String>();
		ArrayList<String> digitalActive = new ArrayList<String>();
		ArrayList<String> digitalDeactive = new ArrayList<String>();
		ArrayList<String> cableActive = new ArrayList<String>();
		ArrayList<String> cableDeactive = new ArrayList<String>();
		
		for (Plan p : account.getPlans()) {
			if(p.getPlanCode() == PlanCode.MobileVoice){
				if(p.getEndDate() == null)
					addFeatureToList(mobileActive, p);
				else
					addFeatureToList(mobileDeactive, p);			
			}	
			else if(p.getPlanCode() == PlanCode.DigitalVoice){
				if(p.getEndDate() == null)
					addFeatureToList(digitalActive, p);
				else
					addFeatureToList(digitalDeactive, p);			
			}		
			else if(p.getPlanCode() == PlanCode.CableTV){
				if(p.getEndDate() == null)
					addFeatureToList(cableActive, p);
				else
					addFeatureToList(cableDeactive, p);			
			}
		}

		List mobileVoice = new List(mobileActive.size() + mobileDeactive.size());
		List digitalVoice = new List(digitalActive.size() + digitalDeactive.size());
		List cableTv = new List(cableActive.size() + cableDeactive.size());

		
		loadList(mobileActive,mobileVoice);
		loadList(mobileDeactive,mobileVoice);
		loadList(digitalActive,digitalVoice);
		loadList(digitalDeactive,digitalVoice);
		loadList(cableActive,cableTv);
		loadList(cableDeactive,cableTv);		
				
		Panel p = new Panel(new BorderLayout());
		Panel p1 = new Panel(new BorderLayout());
		Panel p2 = new Panel(new BorderLayout());
		BoxLayout b = new BoxLayout(this, BoxLayout.Y_AXIS);
		b.preferredLayoutSize(this);
		this.setLayout(b);

		p.setMaximumSize(d);
		p1.setMaximumSize(d);
		p2.setMaximumSize(d);
		
		p.add("North", new Label(AppConstant.DIGITAL_VOICE_PLAN));
		p.add("Center", digitalVoice);
		this.add(p);

		p1.add("North",new Label(AppConstant.MOBILE_VOICE_PLAN));
		p1.add("Center",mobileVoice);
		this.add(p1);

		p2.add("North",new Label(AppConstant.CABLE_TV_FEATURE));
		p2.add("Center",cableTv);
		this.add(p2);
		this.validate();
		this.repaint();
	}

	private void loadList(ArrayList<String> strList, List list) {
		for (String string : strList) {			
			list.add(string);
		}
	}

	private void addFeatureToList(ArrayList<String> list, Plan p)
			throws CloneNotSupportedException {
		list.add(p.toString());
		for (Feature f : p.getFeatures()) {
			list.add("     "+f.toString());
			}
	}
}
